package com.cn.servlet.login;

import com.cn.Dao.AdminDao;
import com.cn.Dao.DepDao;
import com.cn.Dao.UserDao;
import com.cn.entity.AdminInfo;
import com.cn.entity.DepInfo;
import com.cn.entity.UserInfo;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LoginServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        String message="";

        try{
            DepDao depDao=new DepDao();
            List<DepInfo> depInfos=new ArrayList<>();
            depInfos=depDao.findAll();
            request.getSession().setAttribute("depInfos",depInfos);
            AdminDao adminDao=new AdminDao();
            UserDao userDao=new UserDao();
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            AdminInfo adminInfo=new AdminInfo();
            UserInfo userInfo=new UserInfo();
            if (username == null||password==null) {
                throw new NullPointerException();
            }
            adminInfo.setUsername(username);
            adminInfo=adminDao.findByName(adminInfo);
            userInfo.setUsername(username);
            userInfo=userDao.findByName(userInfo);
            if(adminInfo.getUsername()!=null||userInfo.getUsername()!=null){
                if(adminInfo.getUsername()!=null&&adminInfo.getPassword().equals(password)){
                    message="登陆成功";
                    request.setAttribute("message", message);
                    //get user list
                    List<UserInfo> userInfos=userDao.findAll();
                    //set session
                    request.getSession().setAttribute("adminInfo",adminInfo);
                    request.getSession().setAttribute("userInfos",userInfos);
                    request.getRequestDispatcher("admin.jsp").forward(request, response);
                }
                else if(userInfo.getUsername()!=null&&userInfo.getPassword().equals(password)){
                    message="登陆成功";
                    request.setAttribute("message", message);
                    request.getSession().setAttribute("userInfo",userInfo);

                    request.getRequestDispatcher("user.jsp").forward(request, response);
                }
                else {
                    message="登陆失败,密码错误";
                    request.setAttribute("message", message);
                    request.getRequestDispatcher("login.jsp").forward(request, response);
                }
            }else{
                message="登陆失败,账户不存在";
                log(username+" "+password+" "+userInfo.getId());
                request.setAttribute("message", message);
                request.getRequestDispatcher("login.jsp").forward(request, response);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();

            message="登陆失败";
            request.setAttribute("message", message);
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

    }
}
